﻿/*
 *  copyright (c) by potatoproject
 * 
 *  author : potato (nq.wangxiaobo@gmail.com)
 * 
 *  created date : 7/11/2009 5:00:31 PM
 * */
using System;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace BMUpload.Utility
{
    public class FileHelper
    {
        public FileHelper()
        {
        }

        /// <summary>
        /// 将指定文件读成字节数组并返回
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static byte[] GetBytes(string fileName)
        {
            FileInfo file = new FileInfo(fileName);
            FileStream fs = file.OpenRead();
            byte[] result = new byte[fs.Length];
            fs.Read(result, 0, result.Length);
            return result;
        }

        public static void UpLoadFile(byte[] file, string url, NetworkCredential credential)
        {
            WebClient wc = new WebClient();
            wc.Credentials = credential;
            wc.UploadData(url, "PUT", file);
        }

        public static void DownLoadFile(string url, string fileName, string localFile, NetworkCredential credential)
        {
            WebClient wc = new WebClient();
            wc.Credentials = credential;
            wc.DownloadFile(url + fileName, localFile);
        }

        public static void WriteFile(string fileContent, string fileName)
        {
            System.IO.StreamWriter sw = new StreamWriter(fileName);
            sw.Write(fileContent);
            sw.Close();
        }

        public static void WriteFile(byte[] bytes, string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fs.Write(bytes, 0, bytes.Length);
            }

        }

        public static void DeleteFile(string fileName)
        {
            System.IO.File.Delete(fileName);
        }

        public static string OpenFile(string filter,string initpath)
        {
            string resultFile=string.Empty ;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = initpath;// "D:\\Patch";
            openFileDialog1.Filter = filter;// "PDF文件 (*.PDF)|*.PDF|所有(*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                resultFile = openFileDialog1.FileName;
            }

            return resultFile;
        }

        public static string FileToBase64Code(string filepath)
        {
            System.IO.FileStream fs = System.IO.File.OpenRead(filepath);//传文件的路径即可
            System.IO.BinaryReader br = new BinaryReader(fs);
            byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
            string base64String = Convert.ToBase64String(bt);
            br.Close();
            fs.Close();

            return base64String;

        }
        /// <summary>
        /// 获取文件大小
        /// 以KB为单位
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static long  FileLenth(string filepath)
        {
            System.IO.FileInfo f = new FileInfo(filepath);
            return  f.Length/1024;
        }
    }
}
